<?php
/**
 * Created by PhpStorm.
 * User: DongYao
 * Date: 2017/12/15
 * Time: 上午10:47
 */

namespace App\Support\ConfigSet;

use Illuminate\Support\Facades\View;

class ConfigSet
{
    public $app;

    public function __construct($app)
    {
        $this->app = $app;
    }

    /**
     * 写入配置文件
     * @param $name
     * @param $data
     * @return bool
     */
    protected function write($name, $data)
    {
        $file = config_path() . DIRECTORY_SEPARATOR . $name . '.php';

        $view = View::file(__DIR__ . '/template.blade.php', ['configs' => $data]);
        $content = "<?php\r\n\r\n" . $view->render();

        $res = @file_put_contents($file, $content);

        if ($res !== false) {
            $res = true;
        }

        return $res;
    }

    /**
     * 保存配置参数到文件
     * @param $name
     * @param $configs
     * @return bool
     */
    public function save($name, $configs)
    {
        return $this->write($name, $configs);
    }
}